@extends('Layouts.iframe')
<style>
    .width100{
        width: 100% !important;
    }
    .dispose_block li{
        height: 50px;
    }
    label.error{
        height: 20px;
        line-height: 20px;
        left: 70px;
        top: 36px;
    }
    .handlingResult label{
        text-align: left;
        left: 90px;
    }
    .Validform_error, input.error, select.error, textarea.error{
        background-color: #FFEAEA !important;
    }
</style>
@section('content')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span> <a href="{{url('admin/order')}}">签单管理</a>
        <span class="c-gray en">&gt;</span> 处理签单
        {{--<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" >--}}
        <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="{{url('admin/order')}}" title="刷新" >
            <i class="Hui-iconfont">&#xe68f;</i></a>
    </nav>
    <form action="" method="post" class="form form-horizontal" id="form-permission-add">
    <div class="dispose">
        <div class="dispose_block">
            <p class="title">渠道信息</p>
            <ul>
                <li>录单人：{{$order->channel_info['manager']}}</li>
                <li>录入时间：{{$order->created_at}}</li>
                <li>最新状态：{{$order->created_at}}</li>
            </ul>
        </div>
        <div class="dispose_block">
            <p class="title">用户信息</p>
            <ul>
                <li>学生姓名：<input type="text" name="customer" id="customer"  value="{{$order->userinfo['customer']}}"></li>
                <li>联系电话：<input type="text" name="phone" id="phone" value="{{$order->userinfo['phone']}}"></li>
                <li>身份证号：<input type="text" name="idcard" id="idcard" value="{{$order->userinfo['idcard']}}"></li>
                <li>申请专业：</li>
                <li>家长姓名：<input type="text" name="idcard" id="idcard" value="{{$order->userinfo['idcard']}}"></li>
                <li>家长电话：<input type="text" name="idcard" id="idcard" value="{{$order->userinfo['idcard']}}"></li>
            </ul>
        </div>
        <div class="dispose_block">
            <p class="title">处理结果</p>
            <div class="handlingResult">
                <label for="">
                    <input type="radio" @if($order->status == 1) checked @else checked @endif name="fangkuan" id="fangkuan" value="1" onclick="checkoff()" style="position: relative;">
                    确认报名
                </label>
                <ul>
                    <li>
                        <span class="min-title" data-title="产品分类"><i class="red">*</i>产品分类：</span>
                        <span class="select-box inline" style="width: 89px;">
                <select name="category_id_first" id="category_id_first" class="select" onchange="changeCategory(value,1)">
                  <option value="0">请选择分类</option>
                        @foreach($categories as $category)
                            <option value="{{$category->id}}" @if(array_key_exists('category_id_first',$order->confirm_loan)){{  $category->id==$order->confirm_loan['category_id_first']?'selected':'' }} @endif>{{$category->name}}</option>
                        @endforeach

                </select>
              </span>
                        <span class="select-box inline" style="width: 89px;">
                <select name="category_id_second" id="category_id_second" class="select" onchange="changeCategory(value,2)">
                  <option value="0">请选择分类</option>
                    @if(array_key_exists('category_id_first',$order->confirm_loan))
                        @if($second_categories!=null)
                            @foreach($second_categories as $second_category)
                                <option value="{{$second_category->id}}" {{$second_category->id==$order->confirm_loan['category_id_second']?'selected':''}}>{{$second_category->name}}</option>
                            @endforeach
                        @endif
                    @endif
                </select>
              </span>
                    </li>
                    <li>
                        <span class="min-title" data-title="所属机构"><i class="red">*</i>校区：</span>
                        <span class="select-box inline" style="width: 180px;">
                <select name="agent_id" id="agent_id" class="select">
                   <option value="">请选择机构</option>
                        @foreach($agents as $agent)
                            <option value="{{$agent->id}}" @if(isset($order->confirm_loan['agent_id'])){{$agent->id==$order->confirm_loan['agent_id']?'selected':''}}@endif selected>{{$agent->name}}</option>
                        @endforeach

                </select>
              </span>
                    </li>

                    <li>
                        <span class="min-title" data-title="放款金额"><i class="red">*</i>缴费金额：</span>
                        <input type="text" name="loan_amount" id="loan_amount" value="{{array_key_exists('loan_amount', $order->confirm_loan)?$order->confirm_loan['loan_amount']:''}}"></li>
                    <li class="width100">
                        <span class="min-title" data-title="佣金"><i class="red">*</i>收费方式：</span>
                        <span class="select-box inline" style="width: 180px;">
                        <select name="fee_type" id="fee_type" class="select">

                          <option value=""  selected>请选择</option>
                          <option value="1" @if(isset($order->confirm_loan['fee_type'])){{$order->confirm_loan['fee_type']=='1'?'selected':''}}  @endif>财务室已收费</option>
                          <option value="2" @if(isset($order->confirm_loan['fee_type'])) {{$order->confirm_loan['fee_type']=='2'?'selected':''}} @endif>报名费代收</option>

                        </select>
              </span>
                        <input type="text" name="fee_value" id="fee_value" value="{{ array_key_exists('fee_value',$order->confirm_loan)? $order->confirm_loan['fee_value']:''}}" style="width: 89px;">
                    </li>
                </ul>
            </div>
            <div class="handlingResult">
                <label for="">
                    <input type="radio" @if($order->status == '-1') checked @endif name="fangkuan"  value="0" onchange="checkon()" style="position: relative;" class="tuidan">
                    意向流失
                </label>
                <textarea name="reason" id="reason" cols="30" placeholder="请输入退单原因，必填(200个字数以内)" rows="10" maxlength="200" style="position: relative;">{{$order->reason}}</textarea>
            </div>
        </div>
        <p class="btn_box">
            {{--<a href="javascript:;" >提交</a>--}}
            {{--<a href="javascript:;">返回</a>--}}
        <div class="col-c col-offset-5">
            <input class="btn btn-primary radius" type="submit"  value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input class="btn btn-default radius" type="button" onclick="javascript:history.back(-1);" value="&nbsp;&nbsp;返回&nbsp;&nbsp;">
        </div>
        </p>
    </div>
    </form>
@endsection
@section('js')
    <script type="text/javascript">
        var _token="{{csrf_token()}}";
        var is_true = 0;
        if($(".tuidan").is(':checked')){
            var is_true = 1;
        }

        function checkon(){
            is_true = 1
        }
        function checkoff(){
            is_true = 0
        }
        console.log(is_true);
        $("#form-permission-add").validate({
            rules:{
                customer:{
                    required:true,
                    maxlength:15,
                    checkNick:true
                },
                phone:{
                    required:true,
                    maxlength:11,
                    digits:true,
                    checkPhone:true
                },
                idcard:{
                    required:true,
                    maxlength:18,
                    digits:true,
                    checkID:true
                },
            },
            messages:{
                customer:{
                    required:'必填',
                    maxlength:'最大长度15位汉字'
                },
                phone:{
                    required:'必填',
                    maxlength:'最大长度11位数字',
                    digits:'必须是整数'
                },
                idcard:{
                    required:'必填',
                    maxlength:'最大长度18位数字',
                    digits:'必须是整数'
                },
                category_id_first:{
                    required:'必选',
                },
                category_id_second:{
                    required:'必选',
                },
                agent_id:{
                    required:'必选',
                },
                product_id:{
                    required:'必选',
                },
                loan_amount:{
                    required:'必填',
                    digits:'必须是整数',
                },
                fee_type:{
                    required:'必选',
                },
                fee_value:{
                    required:'必填',
                    number:'必须是合法的数字',
                },
            },

            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var flag=0;

                if(is_true){
                    $('.dispose textarea').each(function () {
                        if(($(this).val()).length <= 0 || $(this).val() == null || ($(this).val()).length>200){
                            flag++;
                            $(this).addClass('error');
                            $(this).after('<label id="name-error" class="error" for="name" style="display: block;position: relative;margin-bottom: 23px;margin-left: 20px;top:0;font-size: 12px;">请填写原因</label>');
                        }
                    });
                }else{
                    $('.dispose input').each(function () {
                        var name = $(this).siblings('.min-title').data('title');
                        if($(this).val()===''){
                            flag++;
                            $(this).addClass('error');
                            $(this).after('<label id="name-error" class="error" for="name" style="display: block;font-size: 12px;">请填写'+name+'</label>');
                        }
                    });
                    $('.dispose select').each(function () {
                        var name = $(this).siblings('.min-title').data('title');
                        if(!name){
                            var name = $(this).parent().siblings('.min-title').data('title');
                        }
                        if($(this).val()<=0 || $(this).val() == null){
                            flag++;
                            $(this).addClass('error');
                            $(this).after('<label id="name-error" class="error" for="name" style="display: block;font-size: 12px;">请填写'+name+'</label>');
                        }
                    });
                }

                if(flag!==0){
                    return false;
                }
                var id="{{$order->id}}";
                $(form).ajaxSubmit({
                        type: 'post',
                        url: "{{ url('admin/order') }}"+'/'+id,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        data:{_method:'PUT'},
                        success: function (res) {
                            if(res.code==0){
                                layer.msg(res.message, {icon: 1, time: 2000},function () {
                                    location.href="{{url('admin/order')}}";
                                });
                            }else{
                                layer.msg(res.message, {icon: 1, time: 2000});
                            }
                        },
                        error: function (XmlHttpRequest, textStatus, errorThrown) {
                            layer.msg('error!', {icon: 1, time: 2000});
                        }
                    }
                );
            }
        });

        /*一级分类联动二级分类*/
        $('select[name="category_id_first"]').on('change',function () {
            var html="<option value='0'>全部类型</option>";
            var id=$(this).val();
            if(!id){
                $('select[name="category_id_second"]').html(html);
                return false;
            }
            $.post("{{url('admin/getSecondCategory')}}",{'_token':_token,'id':id},function (data) {
                if(data.length>0){
                    $.each(data,function (key,value) {
                        html += "<option value='"+value.id+"'>"+value.name+"</option>";
                    });
                }
                $('select[name="category_id_second"]').html(html);
            });
        });
        //**//
        var namePreg = /^[\u4e00-\u9fa50-9A-Za-z]{2,15}$/;//昵称
        var mobile = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;//电话号码
        var checkID = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;//身份证
        // 身份证号码
        $.validator.addMethod("checkID",function(value,element,params){
            return this.optional(element)||(checkID.test(value));
        },"*请输入正确的身份证号码！");
        //电话号码
        $.validator.addMethod("checkPhone",function(value,element,params){
            return this.optional(element)||(mobile.test(value));
        },"*请输入正确的电话号码！");
        //姓名
        $.validator.addMethod("checkNick",function(value,element,params){
            return this.optional(element)||(namePreg.test(value));
        },"*请输入正确的客户姓名2-15位汉字！");

        function changeCategory(val,type){
            if(val!==''){
                var pid = '';
                if(type == 2){
                    pid = $("#category_id_first").val();
                }
                $.post("{{url('admin/getCategoryProduct')}}",{_token:_token,id:val,type:type,pid:pid},function (data) {
                    var html = '';
                    if(data.data.length>0){
                        $.each(data.data,function (key,value) {
                            html += "<option value='"+value.id+"'>"+value.name+"</option>";
                        });
                    }else{
                        html += "<option value=''>暂无产品</option>";
                    }
                    $('select[name="product_id"]').html(html);
                });
            }
        }

    </script>
@endsection